package com.lyf.emall.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lyf.emall.bean.*;
import com.lyf.emall.mapper.ProductMapper;
import com.lyf.emall.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author Lfy
 * @Date 2022/3/1-15:02
 */
@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    ProductMapper productMapper;


    @Override
    public ResponseResult getRootProductType() {
        List<ProductType> list = productMapper.queryRootProductType();
        return new ResponseResult(200, "获得根分类成功", list);
    }

    @Override
    public ResponseResult getChildrenProductType(Integer parentId) {
        List<ProductType> list = productMapper.queryChildrenProductType(parentId);
        return new ResponseResult(200, "获得子分类成功", list);
    }

    @Override
    public ResponseResult addRootProductType(ProductType productType) {
        System.out.println(productType);
        Integer integer = productMapper.insertProductType(productType);
        if (integer <= 0){
            return new ResponseResult(400, "添加根分类失败");
        }
        return new ResponseResult(200, "添加根分类成功");
    }

    @Override
    public ResponseResult addOtherProductType(ProductType productType) {
        System.out.println(productType);
        Integer integer = productMapper.insertProductType(productType);
        if (integer <= 0){
            return new ResponseResult(400, "添加商品分类失败");
        }
        return new ResponseResult(200, "添加商品分类成功");
    }

    @Override
    public ResponseResult addProduct(ProductList productList) {
        Integer integer = productMapper.insertProduct(productList);
        if (integer <= 0){
            return new ResponseResult(400, "添加商品失败");
        }
        return new ResponseResult(200, "添加商品成功");
    }

    @Override
    public ResponseResult showProduct(Page page) {
        if (page.getPageNo() < 0){
            new ResponseResult(400, "页码不能为负数");
        }
        PageHelper.startPage(page.getPageNo(), page.getPageNum());
        List<ProductList> productLists = productMapper.queryAllProduct();

        PageInfo<ProductList> pageInfo = new PageInfo<>(productLists);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获取商品列表成功", map);
    }

    @Override
    public ResponseResult updateProductStatus(Integer productId, Integer status) {
        Integer s = 0;
        if (status != 1){
            s = 1;
        }
        Integer integer = productMapper.updateProductStatus(productId, s);
        if (integer <= 0){
            return new ResponseResult(400, "修改商品状态失败");
        }
        return new ResponseResult(200, "修改商品状态成功");
    }

    @Override
    public ResponseResult updateProduct(ProductList productList) {
        Integer integer = productMapper.updateProduct(productList);
        if (integer <= 0){
            return new ResponseResult(400, "修改商品信息失败");
        }
        return new ResponseResult(200, "修改商品信息成功");
    }

    @Override
    public ResponseResult getProductByProductName(String productName, Page page) {
        if (!StringUtils.hasText(productName)){
            return new ResponseResult(400, "商品名称不能为空");
        }
        PageHelper.startPage(page.getPageNo(), page.getPageNum());
        List<ProductList> productLists = productMapper.queryProductByProductName(productName);

        PageInfo<ProductList> pageInfo = new PageInfo<>(productLists);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "查询商品成功", map);
    }

    @Override
    public ResponseResult getProductByTypeId(Integer typeId, Page page) {
        PageHelper.startPage(page.getPageNo(), page.getPageNum());
        List<ProductList> productLists = productMapper.queryProductByTypeId(typeId);

        PageInfo<ProductList> pageInfo = new PageInfo<>(productLists);
        Map<String,PageInfo> map = new HashMap<>();
        map.put("list", pageInfo);
        return new ResponseResult(200, "获得该商品分类成功", map);
    }
}
